﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using V5_Common;
using V5_Model;
using V5_BLL;
using System.IO;

namespace V5_EShop.Admin {
    public partial class Admin_Config : BasicPage {
        protected void Page_Load(object sender, EventArgs e) {
            if (!this.IsPostBack) {
                Action = InputHelper.SetString(Request["Action"]).Trim().ToLower();
                switch (Action) {
                    case "1":
                        this.MultiViewList.ActiveViewIndex = 1;
                        break;
                    case "2":
                        this.MultiViewList.ActiveViewIndex = 2;
                        break;
                    case "3":
                        this.MultiViewList.ActiveViewIndex = 3;
                        break;
                    case "4":
                        this.MultiViewList.ActiveViewIndex = 4;
                        break;
                    default:
                        Bind_ConfigEdit();
                        break;
                }
            }
        }

        private void Bind_ConfigEdit() {
            this.MultiViewList.ActiveViewIndex = 0;

            ModelConfig model = new BLLConfig().GetModel();
            if (model != null) {
                this.hfID.Value = model.ID.ToString();
                this.txtSiteTitle.Text = model.SiteTitle;
                this.txtSiteUrl.Text = model.SiteUrl;
                this.txtWebmasterName.Text = model.WebmasterName;
                this.txtWebmasterEmail.Text = model.WebmasterEmail;
                this.txtSiteCopyright.Text = model.SiteCopyright;
                this.txtSiteDescription.Text = model.SiteDescription;
                this.txtSiteKeywords.Text = model.SiteKeywords;
                this.rblSiteState.SelectedValue = model.SiteState.ToString();
                this.txtSiteCloseText.Text = model.SiteCloseText;
                this.ddlMailObject.SelectedValue = model.MailObject.ToString();
                this.txtMailServer.Text = model.MailServer;
                this.txtMailServerUserName.Text = model.MailServerUserName;
                this.txtMailServerPassWord.Text = model.MailServerPassWord;
                this.txtMailDomain.Text = model.MailDomain;
                //
                //this.ddlUrlRewrite.SelectedIndex = this.ddlUrlRewrite.Items.IndexOf(this.ddlUrlRewrite.Items.FindByText(model.UrlRewrite));
                //
                this.txtUploadType.Text = model.UploadType;
                this.txtUploadMwidth.Text = model.UploadMwidth;
                this.txtUploadSwidth.Text = model.UploadSwidth;
                //
                this.txtSiteLogo.ImageUrl = model.SiteLogo;
                this.txtAdPic.ImageUrl = model.Advertising;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e) {
            ModelConfig model = new BLLConfig().GetModel();
            int ID = InputHelper.SetNumber(this.hfID.Value);
            //基本配置
            string SiteTitle = this.txtSiteTitle.Text;
            string SiteUrl = this.txtSiteUrl.Text;
            string WebmasterName = this.txtWebmasterName.Text;
            string WebmasterEmail = this.txtWebmasterEmail.Text;
            string SiteCopyright = this.txtSiteCopyright.Text;
            string SiteDescription = this.txtSiteDescription.Text;
            string SiteKeywords = this.txtSiteKeywords.Text;
            int SiteState = int.Parse(this.rblSiteState.SelectedValue);
            string SiteCloseText = this.txtSiteCloseText.Text;
            int MailObject = int.Parse(this.ddlMailObject.SelectedValue);
            string MailServer = this.txtMailServer.Text;
            string MailServerUserName = this.txtMailServerUserName.Text;
            string MailServerPassWord = this.txtMailServerPassWord.Text;
            string MailDomain = this.txtMailDomain.Text;
            //
            string urlrewrite = "aspx";// this.ddlUrlRewrite.SelectedValue;
            string uploadtype = this.txtUploadType.Text;
            string uploadmwidth = this.txtUploadMwidth.Text;
            string uploadswidth = this.txtUploadSwidth.Text;
            // 


            #region 上传图片Logo
            if (this.txtLogo.PostedFile.ContentLength > 0)
            {
                try
                {
                    string fullFilePath = this.txtLogo.FileName;
                    string fileName = Path.GetFileName(fullFilePath);//文件名
                    string FileDate ="logo";
                    string saveFileName = "UploadFiles/" + FileDate + "/" + fileName;
                    string savePath = "UploadFiles/" + FileDate + "/";
                    string UploadFilesPath = Server.MapPath("~/");
                    if (!Directory.Exists(UploadFilesPath + savePath))
                    {
                        Directory.CreateDirectory(UploadFilesPath + savePath);
                    }
                    UploadFilesPath += saveFileName;
                    this.txtLogo.SaveAs(UploadFilesPath);
                    model.SiteLogo = "/"+saveFileName;
                }
                catch (Exception ex)
                {
                    model.SiteLogo = "";
                }
            }
            else
            {
                model.SiteLogo = this.txtSiteLogo.ImageUrl;
            }
            #endregion 


            #region 上传图广告
            if (this.txtAd.PostedFile.ContentLength > 0)
            {
                try
                {
                    string fullFilePath = this.txtAd.FileName;
                    string fileName = Path.GetFileName(fullFilePath);//文件名
                    string FileDate = "poster";
                    string saveFileName = "UploadFiles/" + FileDate + "/" + fileName;
                    string savePath = "UploadFiles/" + FileDate + "/";
                    string UploadFilesPath = Server.MapPath("~/");
                    if (!Directory.Exists(UploadFilesPath + savePath))
                    {
                        Directory.CreateDirectory(UploadFilesPath + savePath);
                    }
                    UploadFilesPath += saveFileName;
                    this.txtAd.SaveAs(UploadFilesPath);
                    model.Advertising = "/" + saveFileName;
                }
                catch (Exception ex)
                {
                    model.Advertising = "";
                }
            }
            else
            {
                model.Advertising = this.txtAdPic.ImageUrl;
            }
            #endregion 



            if (ID > 0) {
                model.ID = ID;
                model.SiteTitle = SiteTitle;
                model.SiteUrl = SiteUrl;
                model.WebmasterName = WebmasterName;
                model.WebmasterEmail = WebmasterEmail;
                model.SiteCopyright = SiteCopyright;
                model.SiteDescription = SiteDescription;
                model.SiteKeywords = SiteKeywords;
                model.SiteState = SiteState;
                model.SiteCloseText = SiteCloseText;
                model.MailObject = MailObject;
                model.MailServer = MailServer;
                model.MailServerUserName = MailServerUserName;
                model.MailServerPassWord = MailServerPassWord;
                model.MailDomain = MailDomain;
                //
                model.UrlRewrite = urlrewrite;
                model.UploadType = uploadtype;
                model.UploadMwidth = uploadmwidth;
                model.UploadSwidth = uploadswidth;
                // 

                new BLLConfig().Update(model);
                Response.Redirect(HttpsHelper.ComeUrl);
            }
        }
    }
}
